package com.ytxg.health_management.likeRecords.service.impl;

import com.ytxg.health_management.article.mapper.ArticleMapper;
import com.ytxg.health_management.comment.mapper.CommentMapper;
import com.ytxg.health_management.likeRecords.mapper.LikeRecordsMapper;
import com.ytxg.health_management.likeRecords.entity.LikeRecords;
import com.ytxg.health_management.mapper.UserMapper;
import com.ytxg.health_management.notice.entity.Notice;
import com.ytxg.health_management.notice.mapper.NoticeMapper;
import com.ytxg.health_management.pojo.dto.UserDto;
import com.ytxg.health_management.article.pojo.ArticleMain;
import com.ytxg.health_management.pojo.entity.Comments;
import com.ytxg.health_management.pojo.entity.Page;
import com.ytxg.health_management.pojo.entity.Result;
import com.ytxg.health_management.likeRecords.service.LikeRecordsService;
import com.ytxg.health_management.util.MyException;
import com.ytxg.health_management.webSocket.WebSocketServer;
import com.ytxg.health_management.webSocket.entity.WebSocketMessage;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class LikeRecordsServiceImpl implements LikeRecordsService {

    private static final Logger log = LoggerFactory.getLogger(LikeRecordsServiceImpl.class);
    @Resource
    LikeRecordsMapper likeRecordsMapper;

    @Resource
    ArticleMapper articleMapper;
    @Resource
    CommentMapper commentMapper;
    @Resource
    NoticeMapper noticeMapper;
    @Resource
    UserMapper userMapper;

    /**
     * 添加文章或评论点赞记录
     */
    @Override
    @Transactional
    public Result<String> addLike(LikeRecords likeRecords, UserDto userDto) {
        //判断是否点赞
        if (likeRecords.getLikeType() == 1) {
            LikeRecords r1 = new LikeRecords(null, likeRecords.getArticleId(), null, likeRecords.getUserId(), 1, null);
            List<LikeRecords> likeRecords1 = likeRecordsMapper.selectLikeRecordBase(r1);
            if (!likeRecords1.isEmpty()) {
                throw new MyException("文章已点赞");
            }
        } else {
            LikeRecords r1 = new LikeRecords(null, null, likeRecords.getCommentId(), likeRecords.getUserId(), 2, null);
            List<LikeRecords> likeRecords1 = likeRecordsMapper.selectLikeRecordBase(r1);
            if (!likeRecords1.isEmpty()) {
                throw new MyException("评论已点赞");
            }
        }

        try {
            likeRecords.setCreateTime(new Date());
            likeRecordsMapper.addLikeRecord(likeRecords);
        } catch (Exception e) {
            throw new MyException("添加点赞记录异常" + e);
        }
        try {
            if (likeRecords.getLikeType() == 1) {
                articleMapper.updateLike(likeRecords.getArticleId());
            } else {
                commentMapper.updateLike(likeRecords.getCommentId());
            }
        } catch (Exception e) {
            throw new MyException("更新点赞数失败");
        }

        try {
            if (likeRecords.getLikeType() == 1) {
                List<ArticleMain> articleMains = articleMapper.selectArticleMainBase(new ArticleMain().setId(likeRecords.getArticleId()), new Page());

                noticeMapper.insertNotice(new Notice()
                        .setReceiveUserId(articleMains.get(0).getUser().getId())
                        .setSendUserId(userDto.getId())
                        .setText("我给你的文章《" + articleMains.get(0).getTitle() + "》点赞了！")
                        .setCreateTime(new Date())
                        .setRead("0")
                        .setNoticeType("like"));
                WebSocketServer.sendMessageToUser(articleMains.get(0).getUser().getId(),new WebSocketMessage<>()
                        .setData("我给你的文章《" + articleMains.get(0).getTitle() + "》点赞了！")
                        .setType("string")
                );
            } else {
                List<Comments> comments = commentMapper.selectComments(new Comments().setId(likeRecords.getCommentId()));
                noticeMapper.insertNotice(new Notice()
                        .setReceiveUserId(comments.get(0).getUserId())
                        .setSendUserId(userDto.getId())
                        .setText("我给你的评论《" + comments.get(0).getText() + "》点赞了！")
                        .setCreateTime(new Date())
                        .setRead("0")
                        .setNoticeType("comment"));
                WebSocketServer.sendMessageToUser(comments.get(0).getUserId(),new WebSocketMessage<>()
                        .setData("我给你的评论《" + comments.get(0).getText() + "》点赞了！")
                        .setType("string")
                );
            }
        } catch (Exception e) {
            throw new MyException("添加通知失败");
        }


        return Result.success("点赞成功", null);
    }
}